//================================================================================
//read file,get string
//================================================================================

import java.io.*;
import java.util.*;
import java.lang.Math;


public class StringProcess{
	private String version="2013/08/10";

	private int newstringcount;
	private String[] newstring;


	private DBG dbg;
//	private DBG dbg=new DBG("StringProcess.dbg",0x02);
//								_ShowDeltaTime_	=0x01;
//								_ShowOnScreen_	=0x02;

	private int DBG=0;
	final private int _BYTEMODIFYTX_	=0x01;
	final private int _PARSESTRING_		=0x02;
	final private int _GETSTRINGARRAY_	=0x04;

	public StringProcess(){
		newstring=new String[1];
		newstringcount=0;
		DBG=0;
	}

	//================================================================================
	//================================================================================
	public String getVersion(){
		return version;
	}
	//================================================================================
	//DBG & 0x02
	//================================================================================
	/**
	parse file to one line string
	*/

	public String[] parseString(String orgString,String seperateString,String[] removeString){


		newstringcount=0;

		String[] tmpString=orgString.split(seperateString);
		String[] tmpString_1=new String[tmpString.length];

		if(tmpString.length>0){

if((DBG & _PARSESTRING_)==_PARSESTRING_){
dbg.save("-- DBG parseString ----------------------");
}
			for(int i=0;i<tmpString.length;i++){
				//remove mark part
				while(tmpString[i].indexOf("//")!=-1){

					String tuse="";

					//in the last(with \r\n)--------------------------------------
					if(tmpString[i].indexOf("\r\n") == tmpString[i].indexOf("//")+"//".length()){	
//dbg.save("check_1");

						tuse="//\r\n";
					}
					//not in the last----------------------------------
					else if(tmpString[i].indexOf("\r\n",tmpString[i].indexOf("//")+1) > tmpString[i].indexOf("//")){	
//dbg.save("check_2");
//dbg.save("1:"+tmpString[i].indexOf("\r\n"));
//dbg.save("2:"+tmpString[i].indexOf("//")+"//".length());
						tuse=tmpString[i].substring(tmpString[i].indexOf("//"),tmpString[i].indexOf("\r\n",tmpString[i].indexOf("//")+1)+"\r\n".length());
					}
					//in the last(no \r\n)--------------------------------------
					else{
						tuse=tmpString[i].substring(tmpString[i].indexOf("//"),tmpString[i].length());
					}


					tmpString[i]=tmpString[i].replace(tuse,"");
				}
				//remove mark part


				for(int j=0;j<removeString.length;j++){
//dbg.save("check_3 removeString["+j+"]="+removeString[j]);
					while(tmpString[i].indexOf(removeString[j])!=-1){

						tmpString[i]=tmpString[i].replaceFirst(removeString[j],"");
					}
				}
				
				if(!tmpString[i].equals("")){//if not empty string save tmp save
					tmpString_1[newstringcount]=tmpString[i];
					newstringcount++;
				}

			}
			
			newstring=new String[newstringcount];


			for(int i=0;i<newstringcount;i++){
				newstring[i]=tmpString_1[i];



if((DBG & _PARSESTRING_)==_PARSESTRING_){
dbg.save("newstring["+i+"]="+newstring[i]);
}
			}
if((DBG & _PARSESTRING_)==_PARSESTRING_){
dbg.save("newstring.length="+newstring.length);
}
		}

		return newstring;
	}
	//================================================================================
	//DBG & 0x04
	//================================================================================
	/**
	get part of string
	*/

	public String[] getStringArray(String orgString,String seperateString,String[] removeString){
		ArrayList<String> tmpStringList= new ArrayList<String>(0);
		newstringcount=0;

		if(removeString!=null){
			if(removeString.length!=0){
				for(int j=0;j<removeString.length;j++){
//dbg.save("check_3 removeString["+j+"]="+removeString[j]);
					while(orgString.indexOf(removeString[j])!=-1){
						orgString=orgString.replaceFirst(removeString[j],"");
					}
				}
			}
		}
		
		if(seperateString!=null && orgString!=null){
			if(seperateString.length()!=0 && orgString.length()!=0){
		
				String[] tmpString=null;
				tmpString=orgString.split(seperateString);

				if(tmpString!=null){
					if(tmpString.length>0){
						for(int j=0;j<tmpString.length;j++){
							tmpStringList.add(tmpString[j]);
						}
					}
				}
			}
		}

		if(tmpStringList.isEmpty()){
			newstring=new String[0];
		}
		else{
			newstring=new String[tmpStringList.size()];
			for(int m=0;m<tmpStringList.size();m++){
				newstring[m]=tmpStringList.get(m);
//System.out.println("newstring["+m+"]="+newstring[m]);
			}

		}

		return newstring;

	}
	//================================================================================
	//================================================================================
	public String getPartOfString(String inputString,String TargetString,String seperateString){
		String tmpSting;

	

		if(inputString.equals(null) || TargetString.equals(null) || seperateString.equals(null)){
			return null;
		}


		tmpSting=inputString.replace(seperateString,"");

		if(tmpSting.indexOf(TargetString)==-1){//not exist
			return null;
		}
		else if((tmpSting.indexOf(TargetString)+TargetString.length())==tmpSting.length()){
			return "";
		}
		else{
			inputString=inputString.substring(inputString.indexOf(TargetString),inputString.length());
			if(inputString.indexOf(seperateString)==-1){
				return inputString.substring(TargetString.length(),inputString.length());
			}
			else{
				return inputString.substring(TargetString.length(),inputString.indexOf(seperateString));
			}
		}

	}
	//================================================================================
	//================================================================================
		byte[] bytearray={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};

	private byte getHalfHex(byte inputbyte){
//System.out.println("inputbyte="+inputbyte);
		if(inputbyte>=0x30 && inputbyte<=0x39){//0~9
//System.out.println("0");
			return (byte)(inputbyte-0x30);
		}
		else if(inputbyte>=0x41 && inputbyte<=0x46){//A~F
//System.out.println("1");
			return (byte)bytearray[inputbyte-0x41+10];
		}
		else if(inputbyte>=0x61 && inputbyte<=0x66){//a~f
//System.out.println("2");
			return (byte)bytearray[inputbyte-0x61+10];
		}
		else{
//System.out.println("3");
			return 0x00;
		}
	}

	//================================================================================
	//byte-int transform 
	//http://rritw.com/a/JAVAbiancheng/JAVAzonghe/20120828/213053.html
	//================================================================================

	public int byteToInt(byte inbyte){
		int rInt=(inbyte & 0xFF);
		return rInt;
	}
	public int byteToInt(byte[] inbyte,int lowByteFirst){
		int rInt=0;
		
		if(lowByteFirst>0){//030201=197121
			for(int i=(inbyte.length-1);i>0;i--){
				rInt+=(inbyte[i] & 0xFF)*(0x100<<((i-1)*8));
//System.out.println("rInt_1="+rInt+"   i="+i);
			}
			rInt+=(inbyte[0] & 0xFF);
//System.out.println("rInt_1_last="+rInt);
		}
		else{		//010203=66051
			rInt=(inbyte[inbyte.length-1] & 0xFF);
//System.out.println("rInt_0_first="+rInt);
			for(int i=1;i<(inbyte.length);i++){
				rInt+=(inbyte[inbyte.length-i-1] & 0xFF)*(0x100<<((i-1)*8));
//System.out.println("rInt_0="+rInt+"   i="+i);
			}
//System.out.println("rInt_0_last="+rInt);

		}

		return rInt;
	}
//bytearray
	public byte IntToByte(int ins){
		byte rbyte=0x00;
		if(ins>255)
			ins%=255;

		if((ins%16)>0)
			rbyte|=(byte)bytearray[ins%16];

		rbyte<<=4;

		if((ins/16)>0)
			rbyte|=(byte)bytearray[ins/16];

		return rbyte;
	}
	//================================================================================
	//================================================================================
	public byte[] stringToByte(String orgString){
		byte[] newbyte;
		byte[] tmpbyte=new byte[0];

		if(orgString==null || orgString=="")//return len=0 array
			return tmpbyte;

		orgString.toUpperCase();
		

		if(orgString.length()%2==0){	//even
			newbyte=new byte[(orgString.length()/2)];
			tmpbyte=orgString.getBytes();
			for(int i=0;i<newbyte.length;i++){
				newbyte[i]|=getHalfHex(tmpbyte[i*2]);
				newbyte[i]<<=4;
				newbyte[i]|=getHalfHex(tmpbyte[i*2+1]);
//System.out.println("0:newbyte["+i+"]:"+newbyte[i]);

			}
		}
		else{				//odd
			orgString=orgString+"0";
			newbyte=new byte[orgString.length()/2];
			tmpbyte=orgString.getBytes();
			for(int i=0;i<newbyte.length;i++){
				newbyte[i]|=getHalfHex(tmpbyte[i*2]);
				newbyte[i]<<=4;
				newbyte[i]|=getHalfHex(tmpbyte[i*2+1]);
//System.out.println("1:newbyte["+i+"]:"+newbyte[i]);

			}
		}

		return newbyte;
	}
	//================================================================================
	//================================================================================
	private String[] bytestring={	"0","1","2","3","4","5","6","7",
					"8","9","A","B","C","D","E","F"};
	public String byteToString(byte[] inputbyte){
		String back="";
//System.out.println("inputbyte[0]:"+inputbyte[0]);
		if(inputbyte.length>0){

			for(int i=0;i<inputbyte.length;i++){
//System.out.println("inputbyte["+i+"]:"+inputbyte[i]);
				back+=bytestring[(inputbyte[i]&0xF0)>>4];
				back+=bytestring[inputbyte[i]&0x0F];
			}
		}
		return back;
	}
	//==================================================================
	//note:convert byte to show "hex" format
	//in :-
	//out:-
	//==================================================================
	public String byteToString(byte inputbyte){
		String back="";
		back+=bytestring[(inputbyte&0xF0)>>4];
		back+=bytestring[inputbyte&0x0F];
		return back;
	}	//================================================================================
	//================================================================================
	public String byteToStringAdd(byte[] inputbyte,String s){
		String back="";
//System.out.println("inputbyte[0]:"+inputbyte[0]);
		if(inputbyte.length>0){

			for(int i=0;i<inputbyte.length;i++){
				if((i%16)==0){
					back+="\r\n";
				}
//System.out.println("inputbyte["+i+"]:"+inputbyte[i]);
				back+=s;

				back+=bytestring[(inputbyte[i]&0xF0)>>4];
				back+=bytestring[inputbyte[i]&0x0F];
			}
		}
		return back;
	}
	//================================================================================
	//================================================================================
	public String byteToStringAdd(byte inputbyte,String s){
		String back="";
		back+=s;
		back+=bytestring[(inputbyte&0xF0)>>4];
		back+=bytestring[inputbyte&0x0F];
		return back;
	}
	//================================================================================
	//================================================================================
	private byte[] byteTxMin=new byte[0],byteTxMax=new byte[0],byteTxUnit=new byte[0],byteTx=new byte[0];
	private int[] intTxIndex=new int[0],intTxFun=new int[0];
	private byte ignorefirst=0;

		final int _FUN_RANDOM_		=0x0001;
		final int _FUN_SERIES_		=0x0002;

	public byte[] byteModifyTx(byte[] in,String modify,boolean start){
		final int _INDEX_	=0;
		final int _STARTVALUE_	=1;
		final int _ENDVALUE_	=2;
		final int _CALC_	=3;

		byte[] tmpbytearray;
		int modifyCount=0;


if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("in\t="+this.byteToString(in));
}

		if(start){
			modify=modify.toUpperCase();
			String[] item=modify.split("\\(|\\)");
			//count modify number------------------------------------------
			for(int i=0;i<item.length;i++){

				String[] modifychar=item[i].split("\\/");

				if(modifychar.length==4){
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("item["+i+"]="+item[i]);
}
					modifyCount++;
				}
			}

			if(modifyCount>0){

				intTxIndex	=new int[modifyCount];
				byteTxMin	=new byte[modifyCount];
				byteTxMax	=new byte[modifyCount];
				byteTxUnit	=new byte[modifyCount];
				byteTx		=new byte[modifyCount];

				intTxFun	=new int[modifyCount];

				modifyCount=0;
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("--start------------------");
}
				for(int i=0;i<item.length;i++){

					String[] modifychar=item[i].split("\\/");

					if(modifychar.length==4){
						//modify index---------------------------------------
						tmpbytearray=this.stringToByte(modifychar[_INDEX_]);
						intTxIndex[modifyCount]=tmpbytearray[0];

						//modify min---------------------------------------
						tmpbytearray=this.stringToByte(modifychar[_STARTVALUE_]);
						byteTxMin[modifyCount]=tmpbytearray[0];

						//modify max---------------------------------------
						tmpbytearray=this.stringToByte(modifychar[_ENDVALUE_]);
						byteTxMax[modifyCount]=tmpbytearray[0];
		
						//modify calc---------------------------------------
						byteTxUnit[modifyCount]=0;
						if(modifychar[_CALC_].equalsIgnoreCase("RA")){//random
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("RA----");
}
							intTxFun[modifyCount]|=_FUN_RANDOM_;
						}
						if(modifychar[_CALC_].equalsIgnoreCase("SE")){//series
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("SE-----");
}
							intTxFun[modifyCount]|=_FUN_SERIES_;
						}
						else{
							tmpbytearray=this.stringToByte(modifychar[_CALC_]);
							byteTxUnit[modifyCount]=tmpbytearray[0];
						}

						//modify initial valuse---------------------------------------
						byteTx[modifyCount]=byteTxMin[modifyCount];
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("intTxIndex\t["+modifyCount+"]="+intTxIndex[modifyCount]);
dbg.save("byteTxMin\t["+modifyCount+"]="+this.byteToString(byteTxMin[modifyCount]));
dbg.save("byteTxMax\t["+modifyCount+"]="+this.byteToString(byteTxMax[modifyCount]));
dbg.save("byteTxUnit\t["+modifyCount+"]="+this.byteToString(byteTxUnit[modifyCount]));
dbg.save("byteTx\t\t["+modifyCount+"]="+this.byteToString(byteTx[modifyCount]));
dbg.save("intTxFun\t["+modifyCount+"]="+intTxFun[modifyCount]);
dbg.save();
}

						modifyCount++;
						ignorefirst=1;
					}


				}
			}
		}


if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save();
}
		if(intTxIndex.length>0){
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("--conti------------------");
}


			for(int i=0;i<intTxIndex.length;i++){
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("--before---------------------------------");
dbg.save("intTxIndex\t["+i+"]="+intTxIndex[i]);
dbg.save("byteTxMin\t["+i+"]="+this.byteToString(byteTxMin[i]));
dbg.save("byteTxMax\t["+i+"]="+this.byteToString(byteTxMax[i]));
dbg.save("byteTxUnit\t["+i+"]="+this.byteToString(byteTxUnit[i]));
dbg.save("byteTx\t\t["+i+"]="+this.byteToString(byteTx[i]));
dbg.save("intTxFun\t["+i+"]="+intTxFun[i]);
dbg.save();
}
				if(in.length>=intTxIndex[i] && in.length>0){
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("--trigger---------------------------------");
}

					if(ignorefirst>0){//not modify first
						ignorefirst=0;
						return in;
					}

					//random
					if((intTxFun[i]&_FUN_RANDOM_)==_FUN_RANDOM_){
					//	in[intTxIndex[i]]=(byte)((this.byteToInt(byteTxMax[i])-this.byteToInt(byteTxMin[i]))*Math.random())+byteTxMax[i];
						do{
							in[intTxIndex[i]]=(byte)(this.byteToInt(byteTxMax[i])*Math.random());
							in[intTxIndex[i]]+=byteTxMin[i];
						}
						while(this.byteToInt(in[intTxIndex[i]])>this.byteToInt(byteTxMax[i]));

						byteTx[i]=in[intTxIndex[i]];//for show DBG message
					}
					//just add
					else{
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("--test---------------------------------");
dbg.save("intTxIndex\t["+i+"]="+intTxIndex[i]);
dbg.save("in.length="+in.length);
dbg.save("in\t["+intTxIndex[i]+"]="+in[intTxIndex[i]]);

dbg.save("-----------------------------------------");
}
						in[intTxIndex[i]]=(byte)(byteTxUnit[i]+byteTx[i]);
						if(byteToInt(in[intTxIndex[i]])>byteToInt(byteTxMax[i]))
						{
							in[intTxIndex[i]]=byteTxMin[i];
						}
						byteTx[i]=in[intTxIndex[i]];//save previouse
					}
				}
if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("--after---------------------------------");
dbg.save("intTxIndex\t["+i+"]="+intTxIndex[i]);
dbg.save("byteTxMin\t["+i+"]="+this.byteToString(byteTxMin[i]));
dbg.save("byteTxMax\t["+i+"]="+this.byteToString(byteTxMax[i]));
dbg.save("byteTxUnit\t["+i+"]="+this.byteToString(byteTxUnit[i]));
dbg.save("byteTx\t\t["+i+"]="+this.byteToString(byteTx[i]));
dbg.save("intTxFun\t["+i+"]="+intTxFun[i]);
dbg.save();
dbg.save("-----------------------------------------");
}
			}
		}



if((DBG&_BYTEMODIFYTX_)==_BYTEMODIFYTX_){
dbg.save("out\t="+this.byteToString(in));
dbg.save("-----------------------------------------");
}
		return in;
	}

	//================================================================================
	//================================================================================
	public boolean isStringValid(String inString){

		if(inString==null || inString=="")
			return false;
		else if(inString.length()==0)
			return false;
		else
			return true;
	}
	public boolean isValid(byte[] inbyte){

		if(inbyte==null)
			return false;
		else if(inbyte.length==0)
			return false;
		else
			return true;
	}
	public boolean isInt(String inString){
		boolean rvalue=true;

		if(!isStringValid(inString))
			return false;

		for(int i=0;i<inString.length();i++){
			if(inString.charAt(i)>0x39 || inString.charAt(i)<0x30){
				rvalue=false;
			}
		}

		return rvalue;
	}
	//================================================================================
	//================================================================================
	public boolean isInt(byte inByte){

		boolean rvalue=true;
		
		if((inByte>>4)>0x9 || (inByte&0x0F)>0x9)
		{
			rvalue=false;
		}
				
		return rvalue;
	}
	//================================================================================
	//================================================================================
	public int getDBG(){
		return DBG;
	}
	//================================================================================
	//================================================================================
	public void setDBG(int dbg){
		DBG=dbg;

		if(DBG>0){
			this.dbg=new DBG("StringProcess.dbg",0x03);
		}
	}
	//================================================================================
	//================================================================================
	public void setDBG(int dbg,int fun){
		DBG=dbg;

		if(DBG>0){
			this.dbg=new DBG("StringProcess.dbg",fun);
		}
	}
	//================================================================================
	//================================================================================
/*
	public int[] getFunction(){
		return function;
	}
*/
	//================================================================================
	//================================================================================


}


